﻿; (function (window, undefined, $) {

    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '编号', name: 'ID', width: "5%", sortable: true, hide: true, align: 'left', iskey: true },
                    { display: '文章标题', name: 'Name', width: "30%", sortable: false, align: 'left' },
                    { display: '作者', name: 'Author', width: "10%", sortable: false, align: 'left' },
                    { display: '发布人', name: 'CreateUser', width: "8%", sortable: false, align: 'left' },
                    { display: '发布时间', name: 'PublishTime', width: "10%", sortable: false, align: 'left' },
                    { display: '归属栏目', name: 'Menu', width: "10%", sortable: false, align: 'left' },
                    { display: '点击率', name: 'Hits', width: "6%", sortable: false, align: 'left' },
                    { display: '是否置顶', name: 'IsTop', width: "5%", sortable: false, align: 'left', process: fromatIsTop },
                    { display: '排序', name: 'Sequence', width: "5%", sortable: false, align: 'left' },
                    //{ display: '文章状态', name: 'Status', width: "10%", sortable: false, align: 'left', process: fromatStatus },
                    { display: '操作', name: 'ID', width: "14%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "Id",
            sortorder: "DESC",
            title: false,
            rp: 14,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);
        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='查看' href='javascript:;'  onclick=\"util.Detail('", value, "')\"><i class='fa fa-edit' ></i>查看</a>");
            ops.push("&nbsp;&nbsp;<a title='编辑' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
            ops.push("&nbsp;&nbsp;<a title='删除' class='abtn' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });

        function fromatIsTop(value, cell) {
            if (value == '0') {
                return '否';
            } else if (value == '1') {
                return '是';
            }
        }


        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        $("#btnAdd").click(function (e) {
            var url = options.editUrl;
            location.href = url;
        });
        $("#btnReset").click(function () {
            $("#Name").val(null);
            $('#PublishTime').val(null);
            $("#formQuery").submit();
        }); 
        util.Detail = function (id) {
            var re = /^.*\?.+=.+$/;
            var url = options.detailUrl;
            if (re.test(options.detailUrl)) {
                url += "&id=" + id; 
            } else {
                url += "?id=" + id;
            } 
            location.href = url;
        }; 
        util.Edit = function (id) { 
            var re = /^.*\?.+=.+$/;
            var url = options.editUrl;
            if (re.test(options.detailUrl)) {
                url += "&id=" + id;
            } else {
                url += "?id=" + id;
            }
            location.href = url;
        };
        util.Delete = function (id, name) {
            if (!confirm("您确定要执行删除操作吗？")) {
                return false;
            }
            $.post(options.deleteUrl + "/" + id, { id: id },
                  function (res) {
                      if (res.Status > 0) {
                          alert(res.Message);
                          xjgrid.Reload();
                      }
                      else {
                          alert(res.Message);
                      }
                  },
                  "json"
            );
        };
    });

})(window, undefined, jQuery);